package com.select;

import com.google.gson.Gson;
import com.util.ImgUtils;
import com.util.MyUtil;
import com.bean.jBean;
import com.bean.lineItem;
import com.bean.usersBean;
import com.bean.orderAllBean;
import com.bean.numberBean;
import com.bean.commodityBean;
import com.bean.orderBean;


import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@WebServlet("/myServlet")
@MultipartConfig
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset:utf-8");
        resp.setCharacterEncoding("utf-8");
        /**
         * 生成订单图片
         */
        if("15".equals(type)){
            try {
                String name = req.getParameter("name");
                String amount = req.getParameter("amount");
                String user_name = req.getParameter("user_name");
                String phone = req.getParameter("phone");

                resp.setContentType("image/png");
                //获取批号
                Calendar c = Calendar.getInstance();
                c.add(Calendar.DAY_OF_MONTH,-10);
                String number = new SimpleDateFormat("yyyyMMdd").format(c.getTime());

                BufferedImage img = ImgUtils.imgSave(name,amount+"千克",number,"华育有限公司","1331548455");
                resp.setHeader("Content-Disposition","attachment; filename=image.png");
                ServletOutputStream os = resp.getOutputStream();
                ImageIO.write(img,"PNG",os);
                os.flush();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
           String type = req.getParameter("type");
           req.setCharacterEncoding("utf-8");
           resp.setContentType("text/html;charset:utf-8");
           resp.setCharacterEncoding("utf-8");
        /**
         * 查询全部订单
         */
        if ("1".equals(type)){
               try {
                   String userId = req.getSession().getAttribute("uid").toString();//登录业务员ID

                   Integer a = 0;
                   String name="";

                   usersBean users = MyUtil.selectUser(userId);
                   String status = users.getType();
                   List<orderAllBean> list =  MyUtil.selectAll(a,name,userId,status);
                   for(int b=0;b<=list.size();b++){
                       list.get(b).setUser_id1(req.getSession().getAttribute("uid").toString());
                       list.get(b).setTypes(status);
                   }
                   resp.getWriter().write(new Gson().toJson(list));

               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }
        /**
         * 获取分页页数
         */
        if("12".equals(type)){
            try {
                String name = req.getParameter("name");//商品名
                String begin = req.getParameter("begin");//开始时间
                String finish = req.getParameter("finish");//结束时间

                jBean jbean = MyUtil.selectLimit(name,begin,finish);
                Long line = jbean.getLine();
                Double d=Math.ceil(line*0.1);
                Integer i = d.intValue();
                jbean.setLines(i);
                resp.getWriter().write(new Gson().toJson(jbean));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 根据页数查询数据
         */
        if("13".equals(type)){
            String line = req.getParameter("line");
            String name = req.getParameter("name");
            Integer data = (Integer.valueOf(line)-1)*10;
            String userId = req.getSession().getAttribute("uid").toString();

            try {
                usersBean users = MyUtil.selectUser(userId);
                String status = users.getType();
               List<orderAllBean> list = MyUtil.selectAll(data,name,userId,status);
                for(int b=0;b<list.size();b++){
                    list.get(b).setUser_id1(req.getSession().getAttribute("uid").toString());
                }
               resp.getWriter().write(new Gson().toJson(list));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 设置出库
         */
        if("14".equals(type)){
            String come = "1";
            String id = req.getParameter("id");
            String amount = req.getParameter("amount");//订单商品数量
            String number = req.getParameter("number");//库存编号
            try {
               MyUtil.update(id);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                MyUtil.update(amount,number);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        /**
         * 登录
         */
           if ("2".equals(type)){
               String user = req.getParameter("user");
               String admin = req.getParameter("admin");
            try {
               usersBean usersbean = MyUtil.selectUser(user,admin);
               HttpSession session = req.getSession();
               session.setAttribute("uid",usersbean.getUid());//业务员id
               session.setAttribute("type",usersbean.getType());//业务员的职位 管理员或职员
               System.out.println(usersbean);
               resp.getWriter().write(new Gson().toJson(usersbean));
            } catch (SQLException e) {
                e.printStackTrace();
            }
          }
        /**
         * 根据条件查询订单
         */
        if("3".equals(type)){
                   String name = req.getParameter("name");//商品名
                   String begin = req.getParameter("begin");//开始时间
                   String finish = req.getParameter("finish");//结束时间
               try {
                   List<orderAllBean> list= MyUtil.selectOrder(name,begin,finish);
                   for(int b=0;b<list.size();b++){
                       list.get(b).setUser_id1(req.getSession().getAttribute("uid").toString());
                   }
                   resp.getWriter().write(new Gson().toJson(list));
               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }
           if("4".equals(type)){
                   String sid = req.getParameter("sid");
               try {
                   lineItem lien = MyUtil.selectOrders(sid);
                   resp.getWriter().write(new Gson().toJson(lien));
               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }
           if("5".equals(type)){
               String cas = req.getParameter("cas");
               String name= req.getParameter("name");
               try {
                   List<jBean> list =  MyUtil.selectOrderName(cas,name);
                   resp.getWriter().write(new Gson().toJson(list));
               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }
           if("6".equals(type)){
               String cas = req.getParameter("cas");
               String comm_id = req.getParameter("name");

               String user_id = req.getSession().getAttribute("uid").toString();
               String amount = req.getParameter("quantity");
               String price = req.getParameter("price");
               String cost = req.getParameter("cost");
               String others = req.getParameter("others");//其他成本
               String royalties = "0";//提成
               String bill = req.getParameter("ticket");
               String bill_info = req.getParameter("bill");

               String stockNumber = req.getParameter("stockNumber");
               String status = "0";
               String customerName = req.getParameter("customerName");
               String site = req.getParameter("site");
               String phone = req.getParameter("phone");
               Date date = new Date();
               SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
               String create_time = df.format(date);
               String remark= req.getParameter("remark");
             try {
                   MyUtil.insert(user_id,amount,price,cost,royalties,others,bill,bill_info,comm_id,stockNumber,status,customerName,site,phone,create_time,remark);
               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }
           if("7".equals(type)){
               String sid = req.getParameter("sid");
               try {
                 List<numberBean> list =  MyUtil.selectNumber(sid);
                 resp.getWriter().write(new Gson().toJson(list));
               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }
           if ("8".equals(type)){
               try {
                   List<commodityBean> list = MyUtil.select1();
                   resp.getWriter().write(new Gson().toJson(list));
               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }
        /**
         * 删除订单
         */
        if ("9".equals(type)){
               String userId = req.getSession().getAttribute("uid").toString();
               String did = req.getParameter("radioValue");
               try {
                   MyUtil.delete1(did);
               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }
           if("10".equals(type)){
               String did = req.getParameter("radioValue");
               try {
                 lineItem line =  MyUtil.select3(did);
                 resp.getWriter().write(new Gson().toJson(line));
               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }
           if("11".equals(type)){
                String did = req.getParameter("did");//订单id
                String amount = req.getParameter("amount");//数量
                String price = req.getParameter("price");//单价
                String costPrice = req.getParameter("costPrice");//成本价
                String otherCost = req.getParameter("other_cost");//其他成本
                String bill = req.getParameter("bill");//有无票据
                String billInfo = req.getParameter("bill_info");//票据
                String commodityId = req.getParameter("commodityId");//商品id
                String invoiceId = req.getParameter("invoiceId");//库存编号
                String userName = req.getParameter("userName");//客户名称
                String address = req.getParameter("address");//地址
                String phone = req.getParameter("phone");//联系电话
                String remarks = req.getParameter("remarks");//备注
               double royalties = ((Double.valueOf(amount)*Double.valueOf(price))-(Double.valueOf(amount)*Double.valueOf(costPrice))-Double.valueOf(otherCost))* 0.87 * 0.4;//提成
               try {
                   MyUtil.update(amount,price,costPrice,royalties,otherCost,bill,billInfo,commodityId,invoiceId,userName,address,phone,remarks,did);
               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }

    }
}
